<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
   <TodoHeader @add="addFn"></TodoHeader>

    <!-- 列表区域 -->
 <TodoMain :list="list" @del="delFn"></TodoMain>
    
    <!-- 统计和清空 -->
<TodoFooter :list="list" @clear="clearFn"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  data () {
    return {
      list: JSON.parse(localStorage.getItem('list'))
      //   || [
      //   { id: 1, name: '吃饭' },
      //   { id: 2, name: '睡觉' },
      // ]
    }
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  methods: {
    addFn(value) {
      this.list.unshift({
        id: Date.now(),
        name: value
      })
    },
    delFn(value) {
     this.list = this.list.filter(item=>item.id!=value)
    },
    clearFn() {
      this.list =[]
    }
  },
  watch: {
    list: {
      deep: true,
      handler(value) {
        // console.log(value);
        localStorage.setItem('list', JSON.stringify(value))
      }
    }
  }
}
</script>

<style>

</style>
